package org.zxp.algorithm.sort;

import org.junit.Test;
import org.zxp.algorithm.sort.utils.Tools;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program: effectiveJava
 * @description:
 * @author: X-Pacific zhang
 * @create: 2019-07-28 16:56
 **/
public class JavaSort {
    public static void main(String[] args) throws Exception {
        long time1 = System.currentTimeMillis();
        List list = gen(10000000);
        long time2 = System.currentTimeMillis();
        Collections.sort(list);
        long time3 = System.currentTimeMillis();
        System.out.println("排序元素个数："+list.size());
        System.out.println("gen 耗时："+(time2-time1) + "ms");
        System.out.println("排序 耗时："+(time3-time2) + "ms");
//        list.forEach(o->System.out.println(o));
    }

    public static List<Integer> gen(int n){
        List<Integer> list = new ArrayList<>(n);
        for(int i = 0;i < n;i++){
            int value = new Random().nextInt(n);
            list.add(value);
        }
        return list;
    }

    @Test
    public void testObjSort(){
        User u1 = new User(10,"james");
        User u2 = new User(16,"kobe");
        User u3 = new User(17,"nash");
        User u4 = new User(20,"jorden");
        User u5 = new User(5,"lunnade");
        User u6 = new User(9,"linshuhao");
        Stream<User> stream = Stream.of(u1,u2,u3,u4,u5,u6);
        List<User> l2 = stream.sorted(Comparator.comparing((User::getAge)).reversed()).collect(Collectors.toList());
        l2.forEach(o->System.out.println(o));
    }


    @Test
    public void testPSort(){
        long time1 = System.currentTimeMillis();
        List<Integer> list = gen(10000000);
        Integer[] arr = list.toArray(new Integer[10000000]);
        long time2 = System.currentTimeMillis();
        Arrays.parallelSort(arr);
        long time3 = System.currentTimeMillis();
        System.out.println("排序元素个数："+list.size());
        System.out.println("gen 耗时："+(time2-time1) + "ms");
        System.out.println("排序 耗时："+(time3-time2) + "ms");
    }

    static class User{
        public User(int age, String name) {
            this.age = age;
            this.name = name;
        }

        @Override
        public String toString() {
            return "User{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }

        int age;
        String name;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
